R Examples

Simple DataFrame and ggplot2

Here is a simple data set that is put into an R Data Frame and then plotted using R's ggplot2 library:


In [1]:
year <- c(2000 ,   2001  ,  2002  ,  2003 ,   2004)
rate <- c(9.34 ,   8.50  ,  7.62  ,  6.93  ,  6.60)
df = data.frame(year, rate)

In [2]:
head(df)


Out[2]:
  year rate
1 2000 9.34
2 2001 8.50
3 2002 7.62
4 2003 6.93
5 2004 6.60

In [3]:
library(ggplot2)

In [4]:
ggplot(df, aes(x=year, y=rate)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm)   # Add linear regression line, with 95% confidence region


Out[4]:

Iris data set


In [5]:
head(iris)


Out[5]:
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

In [6]:
pairs(iris[1:4], main = "Iris Data", pch = 21,
      bg = c("red", "green3", "blue")[unclass(iris$Species)])



In [ ]: